<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    protected $fillable=[
        'username','email','password','ip'
    ];
    protected $hidden = [
        'password','email','ip','remember_token','isauth'
    ];

    //关联文章
    public function posts()
    {
        return $this->hasMany('App\Models\Post','user_id','id');
    }
    //获取用户的粉丝
    public function fans()
    {
        return $this->hasMany('App\Models\Fan','star_id','id');
    }
    //获取我关注的fan
    public function stars()
    {
        return $this->hasMany('App\Models\Fan','fan_id','id');
    }
    //我要关注某个人
    public function dofan($user_id)
    {
        $fan=new \App\Models\Fan();
        $fan->star_id=$user_id;
        $this->stars()->save($fan);
    }
    //取消关注
    public function unfan($user_id)
    {
        $fan=new \App\Models\Fan();
        $fan->star_id=$user_id;
        $this->stars()->delete($fan);
    }
    //当前用户是否被关注
    public function hasFan($user_id)
    {
        return $this->fans()->where('fan_id',$user_id)->count();
    }
    //当前用户是否关注了一个人
    public function hasStar($user_id)
    {
        return $this->stars()->where('star_id',$user_id)->count();
    }

    //用户关联通知
    public function notices()
    {
        return $this->belongsToMany('App\Models\Notice','user_notice','user_id','notice_id');
    }
    //增加通知
    public function addNotice($notice)
    {
        return $this->notices()->save($notice);
    }


}
